#include "mysql-connect-factory.h"

struct MysqlConnectFactory {
    char *ip;           // MYSQL 服务器 IP 地址

    char *user;         // 用户名称

    char *password;     // 密码

    char *database;     // 操作数据库的名称

    int port;           // MYSQL 服务器端口
};

MysqlConnectFactory *createMysqlConnectFactory(char *ip, char *user, char *password, char *database, int port) {
    MysqlConnectFactory *factory = malloc(sizeof(MysqlConnectFactory));

    char *p = NULL;

    p = malloc(sizeof(char) * strlen(ip));
    strcpy(p, ip);
    factory->ip = p;

    p = malloc(sizeof(char) * strlen(user));
    strcpy(p, user);
    factory->user = p;

    p = malloc(sizeof(char) * strlen(password));
    strcpy(p, password);
    factory->password = p;

    p = malloc(sizeof(char) * strlen(database));
    strcpy(p, database);
    factory->database = p;

    factory->port = port;

#ifdef DEBUG_MYSQL_CONNECT_FACTORY
    printf("Create Mysql Connect Factory Success!\n");
#endif
    return factory;
}

void destroyMysqlConnectFactory(MysqlConnectFactory *factory) {
    free(factory->ip);
    factory->ip = NULL;

    free(factory->user);
    factory->user = NULL;

    free(factory->password);
    factory->password = NULL;

    free(factory->database);
    factory->database = NULL;

    free(factory);
    factory = NULL;

#ifdef DEBUG_MYSQL_CONNECT_FACTORY
    printf("Destory Mysql Connect Factory Success!\n");
#endif
}

MysqlConnect *createMysqlConnect(MysqlConnectFactory *factory) {
    MysqlConnect *connect = createConnect(factory->ip, factory->user, factory->password, factory->database,
                                          factory->port);
#ifdef DEBUG_MYSQL_CONNECT_FACTORY
    printf("Create Mysql Connect Success!\n");
#endif
    return connect;
}